package app.server.project.main.service.db.entity.cfg;

import app.server.project.core.syndb.SynDBMgr;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;
import java.util.Date;

@Data
@Entity(name = "rank_cfg")
@TableName("rank_cfg")
@Comment(value = "排行榜配置")
public class RankCfgEntity {

    @Id
    @Column(name = "id")
    @Comment("配置主键")
    @TableId("id")
    private int id;

    @Column(name = "next_settlement_time")
    @Comment("下次结算配置时间")
    @TableField("next_settlement_time")
    private Date nextSettlementTime;

    @Column(name = "remark")
    @Comment("备注")
    @TableField("remark")
    private String remark;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;

    public static RankCfgEntity of(int id) {
        var rankCfg = new RankCfgEntity();
        rankCfg.setSynDb(true);
        rankCfg.setId(id);
        rankCfg.setCreateTime(LocalDateTime.now());
        return rankCfg;
    }

    public void setNextSettlementTime(Date nextSettlementTime) {
        this.nextSettlementTime = nextSettlementTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RankCfgEntity::getNextSettlementTime, this);
    }

    public void setRemark(String remark) {
        this.remark = remark;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RankCfgEntity::getRemark, this);
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RankCfgEntity::getCreateTime, this);
    }
}
